/*
 * led_m.c
 *
 *  Created on: Apr 8, 2013
 *      Author: Vincent
 */

#include "gpio.h"
#include "qspi.h"
#include "ledmat.h"
#include "pit0.h"
#include "support_common.h"
#include "n_chuck.h"
#include <stdlib.h>


#define black 0
#define green 1
#define red 2
#define orange 3


uint8 g_green[8];
uint8 g_red[8];
uint8 g_row;
int flag =1;
int next_pattern[8][8];
//int n_pattern[8][8];
int pac_dir=2;
int game_over_flag = 0;
int another_check =0;


int n_pattern [8][8] = {
					{orange, black ,black, black, black, black, black, black},
					{black, black, black, black, black, black, black, black},
					{black, green, black, black, black, black, black, black},
					{black, black, green, green, green, black, black, black},
					{black, black, green, green, black, black, black, black},
					{black, black, green, black, green, black, black, black},
					{black, black, black, black, black, green, black, black},
					{black, black, black, black, black, black, black, red},
									
					};

int o_pattern [8][8] = {
					{orange, black ,black, black, black, black, black, black},
					{black, black, black, black, black, black, black, black},
					{black, green, black, black, black, black, black, black},
					{black, black, green, green, green, black, black, black},
					{black, black, green, green, black, black, black, black},
					{black, black, green, black, green, black, black, black},
					{black, black, black, black, black, green, black, black},
					{black, black, black, black, black, black, black, red},
									
					};

/*int next_pattern[8][8] = {
					{black, black ,black, black, black, black, black, black},
					{black, black, black, black, black, black, black, black},
					{black, black, black, black, black, black, black, black},
					{black, black, green, green, green, black, black, black},
					{black, black, black, black, black, black, black, black},
					{black, black, black, black, black, black, black, black},
					{black, black, black, black, black, black, black, black},
					{black, black, black, black, black, black, black, black},
									
					};*/

int game_over_pattern [8][8] = {
					{green, green ,green, green, green, green, green, green},
					{green, red, black, black, black, black, red, green},
					{green, black, red, black, black, red, black, green},
					{green, black, black, red, red, black, black, green},
					{green, black, black, red, red, black, black, green},
					{green, black, red, black, black, red, black, green},
					{green, red, black, black, black, black, red, green},
					{green, green, green, green, green, green, green, green},
						
					};

void ledmat_init() {
		MCF_GPIO_PQSPAR |= 0x11; // Configure port QS pins 0 and 2 for the primary (QSPI) function
		MCF_GPIO_PTHPAR &= 0x0000; //Configure port TH pins 6 and 7 for the quaternary (GPIO) function.
		
		MCF_GPIO_DDRTH |= 0xC0; // Set Pins 6,7 as output.

		//qspi_init();
		//pit0_init();
		MCF_GPIO_SETTH |= 0x80; // Turn off all of the LED's
		//ledmat_write_pattern();
		//reset_game();
		//ledmat_start();
		//ledmat_restart();
}

void ledmat_refresh()
{
	/*for(int row = 0; row < 8; row++) {
			for(int col = 0; col < 8; col++) {
				n_pattern[row][col] = pattern[row][col];	
			}
		}*/

	ledmat_write_pattern();
	//ledmat_write_row();
	
	
	
}


void ledmat_write_pattern() {
	
	for(int x = 0; x < 8; x++) {
		if (flag == 1)
		{
		g_green[x] = 0x00;
		g_red[x] = 0x00;

		for(int y = 0; y < 8; y++) {
			if(n_pattern[x][y] == red)
				g_red[x] |= (1 << y);
			else if(n_pattern[x][y] == green) 
				g_green[x] |= (1 << y);
			else if(n_pattern[x][y] == orange) 
			{
				g_red[x] |= (1 << y);
				g_green[x] |= (1 << y);
			}
		}
		ledmat_write_row();
		pit0_enable();
		}
		flag = 0;
		while (flag == 0)
		{
			
		}
	}
}

	
void ledmat_write_row() {
	//uint8 inv_g_red [8];
	//uint8 inv_g_green [8];
	/*
	for (int i =0; i<8; i++)
		{
			 for (int j = 7; j>=0; j--)
			 {
				 inv_g_red [i] = g_red[j];
			 }
		}
	for (int i =0; i<8; i++)
	{
		 for (int j = 7; j>=0; j--)
		 {
			 inv_g_green [i] = g_green[j];
		 }
	}*/
		
		uint8 mosi_data[3];
		mosi_data[0] = g_red[g_row];
		mosi_data[1] = g_green[g_row];
		mosi_data[2] = (uint8) (~(1 << g_row));
		MCF_GPIO_SETTH |= 0x80; //Set TH7 high
		MCF_GPIO_CLRTH &= 0xBF; //Set TH6 low
				
		qspi_xfer(mosi_data);
		
		MCF_GPIO_SETTH |= 0x40; //Set TH6 high
		MCF_GPIO_CLRTH &= 0x7F; //Set TH7 low
		
		g_row = (g_row + 1) %8;
}


void ledmat_restart()
{
	for (int i=0; i<8; i++)
					{
						for (int j=0;j<8; j++)
						{	
							n_pattern[i][j] = o_pattern[i][j];
						}
					}
	
}

void ledmat_off()
{
	MCF_GPIO_SETTH |= 0x80;	// Turns off all LEDs
}

void set_pac_dir(int pd)
{
	pac_dir = pd;
}

int set_ghost_dir()
{
	int y = (rand() % (5));
	return y;
	
}

/*void ledmat_off()
{
	MCF_GPIO_SETTH |= 0x80;	// Turns off all LEDs
}
*/

void playgame(int d[][8])
{
	int pac_x, pac_y, n_pac_x, n_pac_y;
	int ghost_x, ghost_y, n_ghost_x, n_ghost_y;

	for (int i = 0; i <=7; i++)
	{
		for (int j = 0; j<=7; j++)
		{
			next_pattern[i][j] = d[i][j];
			if (d[i][j]== orange) 
				{
					pac_y = j;
					pac_x = i;
					
				} 
					
					if (d[i][j]== red) 
					{
						ghost_y = j;
						ghost_x = i;
					}				
		}
		if(i == 0)
		{
			n_pac_y =pac_y;
			n_pac_x=pac_x;
			n_ghost_y=ghost_y;
			n_ghost_x=ghost_x;
		}
	}
	
					
	if(pac_dir == 0)
	{
		next_pattern [pac_x][pac_y] = orange;
		n_pac_x=pac_x;
		n_pac_y=pac_y;
	}
	
	else if ((pac_dir == 1) && (d[pac_x+1][pac_y] != green) && (pac_x+1 <= 7)) //down success
	{
		next_pattern [pac_x][pac_y]= black;
		next_pattern [pac_x+1][pac_y]= orange;
		n_pac_x = pac_x+1;
		n_pac_y = pac_y;
	}
	else if ((pac_dir == 1) && ((d[pac_x+1][pac_y] == green) || ((pac_x+1) > 7))) // down fail
	{
		next_pattern [pac_x][pac_y]= orange;
		n_pac_x = pac_x;
		n_pac_y = pac_y;
	}
	else if ((pac_dir == 2) && (d[pac_x][pac_y-1] != green) && (pac_y-1 >= 0)) //left success
	{
		next_pattern [pac_x][pac_y]= black;
		next_pattern [pac_x][pac_y-1]= orange;
		n_pac_x = pac_x;
		n_pac_y = pac_y-1;
	}
	else if ((pac_dir == 2) && ((d[pac_x][pac_y-1] == green) || (pac_y-1 < 0))) // left fail
		{
			next_pattern [pac_x][pac_y]= orange;
			n_pac_x = pac_x;
			n_pac_y = pac_y;
		}
	else if ((pac_dir == 3) && (d[pac_x-1][pac_y] != green) && (pac_x-1 >= 0)) //up success
		{
			next_pattern [pac_x][pac_y]= black;
			next_pattern [pac_x-1][pac_y]= orange;
			n_pac_x = pac_x-1;
			n_pac_y = pac_y;
		}
	else if ((pac_dir == 3) && ((d[pac_x-1][pac_y] == green) || (pac_x-1 < 0))) // up fail
			{
				next_pattern [pac_x][pac_y]= orange;
				n_pac_x = pac_x;
				n_pac_y = pac_y;
			}
	else if ((pac_dir == 4) && (d[pac_x][pac_y+1] != green) && (pac_y+1 <= 7)) //right success
			{
				next_pattern [pac_x][pac_y]= black;
				next_pattern [pac_x][pac_y+1]= orange;
				n_pac_x = pac_x;
				n_pac_y = pac_y+1;
			}
	else if ((pac_dir == 4) && ((d[pac_x][pac_y+1] == green) || (pac_y+1 > 7))) // right fail
			{
				next_pattern [pac_x][pac_y]= orange;
				n_pac_x = pac_x;
				n_pac_y = pac_y;
			}
	
	
	int ghost_dir = set_ghost_dir();
	
	if(ghost_dir > 4)
		{ ghost_dir = ghost_dir-1;}
	

	if ((ghost_dir <=1 && ghost_dir>=0) && (d[ghost_x+1][ghost_y] != green) && (ghost_x+1 <= 7)) //down success
	{
		next_pattern [ghost_x][ghost_y]= black;
		next_pattern [ghost_x+1][ghost_y]= red;
		n_ghost_x = ghost_x+1;
		n_ghost_y = ghost_y;
	}
	else if ((ghost_dir <=1 && ghost_dir>=0) && ((d[ghost_x+1][ghost_y] == green) || ((ghost_x+1) > 7))) // down fail
	{
		next_pattern [ghost_x][ghost_y]= red;
		n_ghost_x = ghost_x;
		n_ghost_y = ghost_y;
	}
	else if ((ghost_dir <=2 && ghost_dir > 1) && (d[ghost_x][ghost_y-1] != green) && (ghost_y-1 >= 0)) //left success
	{
		next_pattern [ghost_x][ghost_y]= black;
		next_pattern [ghost_x][ghost_y-1]= red;
		n_ghost_x = ghost_x;
		n_ghost_y = ghost_y-1;
	}
	else if ((ghost_dir <=2 && ghost_dir > 1) && ((d[ghost_x][ghost_y-1] == green) || (ghost_y-1 < 0))) // left fail
		{
			next_pattern [ghost_x][ghost_y]= red;
			n_ghost_x = ghost_x;
			n_ghost_y = ghost_y;
		}
	else if ((ghost_dir <=3 && ghost_dir >2) && (d[ghost_x-1][ghost_y] != green) && (ghost_x-1 >= 0)) //up success
		{
			next_pattern [ghost_x][ghost_y]= black;
			next_pattern [ghost_x-1][ghost_y]= red;
			n_ghost_x = ghost_x-1;
			n_ghost_y = ghost_y;
		}
	else if ((ghost_dir <=3 && ghost_dir >2) && ((d[ghost_x-1][ghost_y] == green) || (ghost_x-1 < 0))) // up fail
			{
				next_pattern [ghost_x][ghost_y]= red;
				n_ghost_x = ghost_x;
				n_ghost_y = ghost_y;
			}
	else if ((ghost_dir <=4 && ghost_dir >3) && (d[ghost_x][ghost_y+1] != green) && (ghost_y+1 <= 7)) //right success
			{
				next_pattern [ghost_x][ghost_y]= black;
				next_pattern [ghost_x][ghost_y+1]= red;
				n_ghost_x = ghost_x;
				n_ghost_y = ghost_y+1;
			}
	else if ((ghost_dir <=4 && ghost_dir >3) && ((d[ghost_x][ghost_y+1] == green) || (ghost_y+1 > 7))) // right fail
			{
				next_pattern [ghost_x][ghost_y]= red;
				n_ghost_x = ghost_x;
				n_ghost_y = ghost_y;
			}
	
	 
		if((pac_dir==1) && /*((next_pattern[ghost_x-1][ghost_y]== red)) &&*/ (ghost_y==pac_y) && ((n_ghost_x == pac_x) || (n_pac_x == ghost_x)))//Pac Down gameover check
		{
			game_over_flag=1;
		}
	
		else if((pac_dir==3) &&  /*((next_pattern[ghost_x+1][ghost_y]== red)) &&*/ (ghost_y==pac_y) && ((n_ghost_x == pac_x) || (n_pac_x == ghost_x))) //Pac up gameover check
		{
			game_over_flag=1;
		}
	
		else if((pac_dir==4) && /*((next_pattern[ghost_x][ghost_y-1]== red)) &&*/  (ghost_x==pac_x) && ((n_ghost_y == pac_y) || (n_pac_y == ghost_y))) //Pac right gameover check
		{
			
			game_over_flag=1;
		}
	
		else if((pac_dir==2) &&  /*((next_pattern[ghost_x][ghost_y+1]== red) ) &&*/  (ghost_x==pac_x) && ((n_ghost_y == pac_y) || (n_pac_y == ghost_y))) //Pac left gameover check
		{
			game_over_flag=1;
		}
	
		
	 
	 if ((game_over_flag==1) || ((n_ghost_x==n_pac_x) && (n_ghost_y==n_pac_y))) // game over check
			{
				for (int i = 0; i <=7; i++)
					{
						for (int j = 0; j <=7; j++)
						{
							next_pattern[i][j] = o_pattern[i][j];//game_over_pattern[i][j];
						}
					}
				game_over_flag=0;
				//pac_dir=2;
		
			}
	
}
